package service

import (
	"chaowan/redis_lib"
	"context"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/dacker-soul/getui/auth"
	"github.com/dacker-soul/getui/publics"
	//"github.com/dacker-soul/getui/push/single"
	"github.com/dacker-soul/getui/push/list"
)

func Getuixx(cids []string, title, body string) {
	ctxLocal := context.Background()
	confLocal := publics.GeTuiConfig{
		AppId:        "YO4Gi8gELX8xVXQNFfM8U", // 个推提供的id，密码等
		AppSecret:    "wBXDl5tBz469V31cuEbtn9",
		AppKey:       "tjsarxpPYc7M1GfBqyOmy4",
		MasterSecret: "hkwikgaJko8lthiO5sZwB",
	}
	tokenLocal, _ := redis_lib.Client.Get(ctxLocal, "getuitokenchaowan").Result()
	if tokenLocal == "" || len(tokenLocal) == 0 {
		result, err := auth.GetToken(ctxLocal, confLocal)
		if err != nil {
			log.Fatalln("error:", err)
		}
		tokenLocal = result.Data.Token
		redis_lib.Client.Set(ctxLocal, "getuitokenchaowan", tokenLocal, 86400*time.Second)
	}

	taskid, _ := redis_lib.Client.Get(ctxLocal, "getuitaskidchaowan").Result()

	if taskid == "" || len(taskid) == 0 {
		notification := publics.Notification{
			Title:       title,
			Body:        body,
			ClickType:   "startapp", // 打开应用首页
			BadgeAddNum: 1,
		}
		//fmt.Println(notification)
		singleParamrw := list.PushListMessageParam{
			RequestId: strconv.FormatInt(time.Now().UnixNano(), 10), // 请求唯一标识号
			Settings: &publics.Settings{ // 推送条件设置
				TTL: 3600000, // 默认一小时，消息离线时间设置，单位毫秒
				Strategy: &publics.Strategy{ // 厂商通道策略，具体看public_struct.go
					Default: 1,
					Ios:     1,
					St:      1,
					Hw:      1,
					Xm:      1,
					Vv:      1,
					Mz:      1,
					Op:      1,
				},
				Speed:        100, // 推送速度，设置100表示：100条/秒左右，0表示不限速
				ScheduleTime: 0,   // 定时推送时间，必须是7天内的时间，格式：毫秒时间戳
			},
			PushMessage: &publics.PushMessage{
				Duration:     "", // 手机端通知展示时间段
				Notification: &notification,
				Transmission: "",
				Revoke:       nil,
			},
		}
		singleResultrw, err := list.PushListMessage(ctxLocal, confLocal, tokenLocal, &singleParamrw)
		//fmt.Println(singleResultrw)
		if err != nil {
			log.Fatalln("error:", err)
		}
		taskid = singleResultrw.Data["taskid"]
		redis_lib.Client.Set(ctxLocal, "getuitaskidchaowan", taskid, 86400*time.Second)
	}
	// 2.2安卓通道和个推通道的普通推送参数
	singleParam := list.PushListCidParam{
		Audience: &publics.Audience{ // 目标用户
			Cid:           cids, // cid推送数组
			Alias:         nil,  // 别名送数组
			Tag:           nil,  // 推送条件
			FastCustomTag: "",   // 使用用户标签筛选目标用户
		},
		TaskId: taskid,
	}
	ok, err := list.PushListCid(ctxLocal, confLocal, tokenLocal, &singleParam)
	fmt.Println(ok)
	//fmt.Println(err)
	if err != nil {
		log.Fatalln("error:", err)
	}
	//fmt.Println(singleResult)
}
